package com.meitianhui.convenitenbuy.util;

import android.os.Environment;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2017/10/27.
 */

public class FileUtils {
    public static boolean delete(String path) {
        return delete(new File(path));
    }

    /**
     * 创建一个文件，创建成功返回true
     *
     * @param filePath
     * @return
     */
    public static boolean createFile(String filePath)
    {
        try
        {
            File file = new File(filePath);
            if (!file.exists())
            {
                if (!file.getParentFile().exists())
                {
                    file.getParentFile().mkdirs();
                }
                return file.createNewFile();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * @param saveDir
     * @return
     * @throws IOException
     * 判断下载目录是否存在
     */
    public static String isExistDir(String saveDir) throws IOException {
        // 下载位置
        File downloadFile = new File(Environment.getExternalStorageDirectory(), saveDir);
        if (!downloadFile.mkdirs()) {
            downloadFile.createNewFile();
        }
        String savePath = downloadFile.getAbsolutePath();
        return savePath;
    }

    public static boolean deleteFile(String sPath) {
        boolean flag = false;
        File file = new File(sPath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    public static boolean delete(File deleteFile) {
        if(!deleteFile.exists()) {
            return false;
        } else {
            boolean retVal = false;
            if(deleteFile.isDirectory()) {
                File[] fs = deleteFile.listFiles();
                retVal = delete(fs);
            }

            retVal = deleteFile.delete();
            return retVal;
        }
    }


    public static boolean delete(File[] deleteFiles) {
        if(deleteFiles == null) {
            return false;
        } else {
            boolean retVal = true;
            File[] var5 = deleteFiles;
            int var4 = deleteFiles.length;

            for(int var3 = 0; var3 < var4; ++var3) {
                File f = var5[var3];
                retVal = delete(f);
                if(!retVal) {
                    break;
                }
            }

            return retVal;
        }
    }


}
